package com.simulation;

import java.util.ArrayList;
import java.util.Vector;

import com.simulation.All.ModelType;

class TNumericalFabric
{
     /* for the fabric itself*/
     ModelType m_modelFlag;
     SourceType m_sourceItem;

     /* the delta of fabric thickness*/
     int m_nx;
     double m_deltaT;
     /* the delta unit*/
     double m_deltaX;
     /* temperature of fibers and liquid water*/
     TwoElementArray m_tFabric[] = new TwoElementArray[All.NX]; /* Tf*/
     /* the relative humidity of each node*/
     double m_RH[] = new double[All.NX];
     /* volumetric heat capacity of the fabric*/
     double m_volumetricHeatCapacity[] = new double[All.NX]; /* (cv)m*/
     double m_specificHeatCapacity[] = new double[All.NX];

     /* thermal conductivity of fabric*/
     double m_thermoConductivity[] = new double[All.NX]; /* Kmix*/
     /* specific area of fabric */
     double m_surfaceVolumeRatio[] = new double[All.NX]; /* Sv */
     double m_thickness;
     /* the effecitive mean contact angel between the fiber and the water*/
     double m_meanFiberContactAngle; /* phi*/
     double m_porosity;
     /* average angle of the capillaries in fabrics*/
     double m_averageCapillaryAngle; /* beta*/
     /* effective radius of pore in fabric*/
     double m_effectivePoreRadius; /* dc*/
     /* effective tortuosity of the fabric for water vapor diffusion*/
     double m_tortuosityWaterVapor; /* tao(g)*/
     /* effective tortuosity of the fabric for liquid water diffusion*/
     double m_tortuosityLiqWater; /* tao(l) */
     /* mass transfer coefficient*/
     double m_coeMassTransfer; /* hl.g*/
     /* the heat exchange coefficient between gas and solid fiber*/
     double m_coeHeatGasFiber; /* hgf*/
     /* mean fibers thermal conductivity */
     double m_meanFiberK;

     /*about the fibers*/
     /* fibers included*/
     ArrayList<TNumericalFiber> m_includedFibers;
     /* the volume fraction of fibers */
     double m_vFibers; /* varepsilon(f)*/

     double m_meanFiberRadius;
     double m_meanFiberDensity;
     double m_meanFiberEmissivity;
     /* sorption constant*/
     double m_sorptionConstant;

     /* the water vapor and liquid water in fabric*/
     /* mean water vapor concentration in the fibers of the fabric*/
     double m_meanCWaterVaporInFibers[] = new double[All.NX]; /* /Cf,g */
     /* mean liquid water concentration in the fibers of the fabric*/
     double m_meanCLiquidWaterInFibers[] = new double[All.NX]; /* /Cf,l */
     /* mean all concentration in the fibers of the fabric*/
     double m_meanCInFibers[] = new double[All.NX]; /* /Cf*/
     /* water vapor content of the fibers in the fabric*/
     double m_wWaterVaporInFibers[] = new double[All.NX]; /* Wca=/Cf,g / rho(f) */
     /* liquid water content of the fibers in the fabrics*/
     double m_wLiquidWaterInFibers[] = new double[All.NX]; /* Wcl=/Cf,l / rho(f) */
     /* water content of the fibers in the fabric */
     double m_meanWWaterInFibers[] = new double[All.NX]; /* Wc*/

     /* the membrane*/
     MembraneType m_includedMembranes[] = new MembraneType[2];

     /* about the gas */
     /* volume fration of gas*/
     TwoElementArray m_vGas[] = new TwoElementArray[All.NX]; /* varepsilon(s)*/
     /* Saturated water vapor concentration in the inter-fiber void space*/
     double m_cSaturatedWaterVapor[] = new double[All.NX]; /* C*a(T)*/
     /* water vapor concentration in the air filling the inter-fiber void spaces */
     TwoElementArray m_cWaterVapor[] = new TwoElementArray[All.NX];  /* Ca*/
     /* coefficient of water vapor*/
     double m_coeWaterVapor[] = new double[All.NX]; /* D=Da* varepsilon(s)*/

     /* the water vapor proportion sorbed by fiber*/
     double m_vSorbtionProportion; /* varpi*/
     /* the water vapor absorbed by the fibers of the fabric*/
     double m_absorbedWaterVapor[] = new double[All.NX]; /* SSS1*/
     /* heat coefficient of soprtion or desorption of water vapor*/
     double m_heatCoeSorptionWaterVapor[] = new double[All.NX]; /* randa(v)*/
     /* thermal conductivity of gas on every grid of fabric */
     double m_thermoConductivityGas[] = new double[All.NX]; /*Kg=kvg* varepsilon(s)*/
     /* the heat loss from the water vapor and liquid water absorbed by the fibers of fabric*/
     double m_heatLossByFibers[] = new double[All.NX]; /* TTT1*/
     /* the volume of evaporated liquid water or condensed water vapor */
     double m_volumeEvapCondensation[] = new double[All.NX]; /* SSS2*/
     /* the heat loss by the process of evaporation or condensation*/
     double m_heatEvapCondensation[] = new double[All.NX]; /* TTT2*/

     /*about liquid water*/
     /* the liquid water proportion sorbed by fiber*/
     double m_lSorbtionProportion; /* varpi*/
     /* volume fraction of liquid water*/
     TwoElementArray m_vLiquidWater[] = new TwoElementArray[All.NX]; /* varepsilon(l)*/
     /* diffusion coefficient of liquid water in the fabric*/
     double m_diffusionCoeLiqWater[] = new double[All.NX]; /* DL*/
     /* the liquid water absorped by the fibers of fabric and condensation of water vapor*/
     double m_liquidWaterItem[] = new double[All.NX]; /* SSSS*/
     /* heat coefficient of sorption or desorption of liquid water by fabric*/
     double m_heatCoeSorptionLiqWater[] = new double[All.NX]; /* randa(l)*/

     /* when adding the pressure*/
     /* the pressure of gas on every grid of fabric*/
     TwoElementArray m_pressureGas[] = new TwoElementArray[All.NX]; /* Ps*/
     /* coefficient of pressure to water vapor and dry gas*/
     double m_coePressureWaterVapor[] = new double[All.NX]; /* G */
     /* coefficient of pressure to liquid water*/
     double m_coePressureLiquidWater[] = new double[All.NX]; /* GL */
     /* coefficient of gas */
     double m_coePressureGas[] = new double[All.NX]; /* GS*/

     double m_coePressureTime[] = new double[All.NX];  /* A3*/

     double m_coePressureTempetratureToTime[] = new double[All.NX]; /* A4*/

     double m_coePressureLiquidWaterTotime[] = new double[All.NX]; /* A5*/

     /* when adding virus*/
     /* virus concentration in the liquid water of the fabric*/
     TwoElementArray m_cVirusInLiqWater[] = new TwoElementArray[All.NX]; /* Cv*/
      /* volume fraction of virus in deposition*/
     TwoElementArray m_vVirus[] = new TwoElementArray[All.NX]; /* varpesilon(v)*/
     /* the coefficient used in equations*/
     double m_coeDiffusionVirus[] = new double[All.NX]; /* DV=Db* varepsilon(v)*/
     double m_coeVirusToPressure[] = new double[All.NX]; /* GV */
     double m_coeVirusToLiqWater[] = new double[All.NX]; /* DLV */
     double m_coePVelocityLiq[] = new double[All.NX]; /* Blp */
     double m_coeVVelocityLiq[] = new double[All.NX]; /* Bll */
     double m_coePVelocityGas[] = new double[All.NX]; /* Bsp */

     /* when adding nonlocal equilibrium */
     /* temperature of gas*/
     TwoElementArray m_tGas[] = new TwoElementArray[All.NX];  /* Tg */
     /* coefficient of local equilibrium of gas*/
     double m_coeLocalEquiGas[] = new double[All.NX]; /* a(i) */
     /* value of local equilibrium of gas*/
     double m_valueLocalEquiGas[] = new double[All.NX]; /* s(i) */
     /* the coefficient item used in equation */
     double m_coeFeTg[] = new double[All.NX];  /* Fe */
     double m_coeFwTg[] = new double[All.NX];  /* Fw */
     double m_coeFweTg[] = new double[All.NX]; /* Fe-Fw */
     double m_coeDDeTg[] = new double[All.NX]; /* DDe */
     double m_coeDDwTg[] = new double[All.NX]; /* DDw */
     double m_coePeTg; /* Pe */
     double m_coePwTg; /* Pw*/
     double m_coeAfeTg[] = new double[All.NX]; /* AFE */
     double m_coeAfwTg[] = new double[All.NX]; /* AFW */

     /* when adding PCM */
     /* volume fraction of PCM */
     double m_vPCM;  /* vareplosin(m) */
     /* heat loss from the micro_PCM*/
     double m_heatLossPCM[] = new double[All.NX]; /* q */
     /* the radius of interface*/
     TwoElementArray m_radiusInterfacePCM[] = new TwoElementArray[All.NX]; /* rl[n]*/
     /* moving velocity of interface in micro-PCM*/
     double m_movingVelocityPCM[] = new double[All.NX]; /* .rl[n]*/

     /* the items in the equation*/
     /* the differential item of Water Vapor Concentration of fabric*/
     double m_differentialWaterVapor[] = new double[All.NX]; /* Cf,g/t*/
     /* the differential item of liquid water concentration of fabric*/
     double m_differentialLiquidWater[] = new double[All.NX]; /* Cf,l/t*/
     /* the differential item of virus of fabric*/
     double m_differentialVirus[] = new double[All.NX]; /* (rho(v)* varpesilon(v)) /t */

     double m_mu; /* delta(t)/ delat2(x)*/
     
     /* all the fluid item in the equation 1 includes the fluid sorpted by fibers and evaporation*/
     double m_waterVaporItem[] = new double[All.NX];  /*ss*/
     double m_temperatureItem[] = new double[All.NX]; /* tt*/


     /* coefficient */
     /* convection mass transfer coefficient */
     double m_coeConvecMassIn, m_coeConvecMassOut;
     /* convection heat transfer coefficient */
     double m_coeConvecHeatIn, m_coeConvecHeatOut;

     /* boundary condition*/
     boolean m_inContact,m_outContact;
     double m_temperatureInner, m_temperatureOuter;
     double m_heatInner,m_heatOuter;
     double m_RHInner,m_RHOuter;
     double m_pressureInner,m_pressureOuter;
     /* the water vapor concentration of inner and outer of fabric*/
     double m_CVaporInner,m_CVaporOuter;
     /* the liquid water concentration of inner and outer of fabric*/
     double m_CLiquidInner,m_CLiquidOuter;
     /* the liquid water fraction of inner and outer of fabric*/
     double m_vLiquidInner,m_vLiquidOuter;

     /* the initial value*/
     double m_initialTInner,m_initialTOuter;
     double m_initialRHIner,m_initialRHOuter;
     double m_initialCInner,m_initialCOuter;

     int m_layNumber;
     TNumericalPCM m_pcm;

     TNumericalFabric(int nx,int nr,ArrayList<TNumericalFiber> includedFibers,ModelType modeflag, SourceType sourceitem, int layNumber) {
         TNumericalFiber fiberItem;

         m_modelFlag=modeflag;
         m_sourceItem=sourceitem;

         m_layNumber=layNumber;
         if(modeflag.compareTo(ModelType.GasEnergy)>=0)//(modeflag>=GasEnergy)
         {
              m_nx=nx;
              m_sourceItem=sourceitem;
              m_includedFibers=includedFibers;
         }
         
         for(int i=0; i<All.NX; i++) {
        	 m_tFabric[i] = new TwoElementArray();
        	 m_vGas[i] = new TwoElementArray();
        	 m_cWaterVapor[i] = new TwoElementArray();
        	 m_vLiquidWater[i] = new TwoElementArray();
        	 m_pressureGas[i] = new TwoElementArray();
        	 m_cVirusInLiqWater[i] = new TwoElementArray();
        	 m_vVirus[i] = new TwoElementArray();
        	 m_tGas[i] = new TwoElementArray();
        	 m_radiusInterfacePCM[i] = new TwoElementArray();
         }
     };
     //~TNumericalFabric();
     
     void InitialValue(TNumericalGas gas,TNumericalLiquidWater liquidWater,FabricType fabric,ControlType m_controls) {
    	 TwoElementArray valueArray;
         TNumericalFiber fiberItem;
         //TList *fiberList;

         if(fabric.SelfHeat)
         {
             m_sourceItem.SelfHeating=true;
         }
         else
         {
             m_sourceItem.SelfHeating=false;
         }

         m_thickness=fabric.Thickness;
         m_vFibers=fabric.VolumePercenFiber;
         m_vPCM=fabric.VolumePercentPCM;

         m_deltaT=m_controls.TimeStep;
         m_deltaX=m_thickness/(m_nx-1);
         m_mu=m_deltaT/Math.pow(m_deltaX,2);

         //m_vFibers=0.15;
         m_coeMassTransfer=liquidWater.m_massTransfer ;
         m_coeHeatGasFiber=0.239*gas.m_heatExchangeCoefficient;

         m_averageCapillaryAngle=fabric.CapAngle;
         m_effectivePoreRadius=fabric.MaxDisCapDia; //5.0e-5;
         m_tortuosityWaterVapor=fabric.TortuosGas;  //1.2;
         m_tortuosityLiqWater=fabric.TortuosLqd; //1.2;

         m_meanFiberRadius= 0;
         m_meanFiberDensity=0;
         m_meanFiberEmissivity=0;
         m_meanFiberContactAngle=0;
         
    	 for(int i=0;i<m_includedFibers.size();i++)
    	 {
    		 fiberItem=m_includedFibers.get(i);
    		 m_meanFiberRadius += fiberItem.m_radius * fiberItem.m_massRatio;
    		 m_meanFiberDensity += fiberItem.m_density * fiberItem.m_massRatio;
    		 m_meanFiberEmissivity += fiberItem.m_emissivity * fiberItem.m_massRatio;
    		 m_meanFiberContactAngle += fiberItem.m_contactAngle * fiberItem.m_massRatio;
    		 //m_meanFiberK += fiberItem.m_thermoConductivity *fiberItem.m_massRatio ;
    	 }

         m_sorptionConstant=m_vFibers*m_meanFiberEmissivity/m_meanFiberRadius;

         m_porosity=1.0-m_vFibers;

         if(m_sourceItem.PCM)
         {
             //m_vPCM=0.1;
             m_porosity=m_porosity-m_vPCM;

         }

         for(int i=0;i<m_nx;i++)
         {
             m_tFabric[i].preNodeValue = m_controls.InitialT;// m_initialTInner-(m_initialTInner-m_initialTOuter)*i/(m_nx-1);

             m_RH[i]=m_controls.InitialRH ;// m_initialRHIner-(m_initialRHIner-m_initialRHOuter)*i/(m_nx-1);

             m_cWaterVapor[i].preNodeValue=TPhysicalPro.WaterVaporConcentration(m_tFabric[i].preNodeValue)*m_RH[i];
             //m_initialCInner-(m_initialCInner - m_initialCOuter)*i/(m_nx-1);

             m_vGas[i].preNodeValue=m_porosity;
             if(m_sourceItem.PCM)
             {
                 m_heatLossPCM[i]=0.0;
                 m_radiusInterfacePCM[i].preNodeValue=0.0;
             }

             if(m_modelFlag.compareTo(ModelType.AddLiquid)>=0)
             {
                 m_vLiquidWater[i].preNodeValue=m_controls.InitialWater;
                 m_vGas[i].preNodeValue -= m_vLiquidWater[i].preNodeValue;
                 m_differentialLiquidWater[i] =0.0;
             }

             if(m_modelFlag.compareTo(ModelType.AddPressure)>=0)
             {
                 m_pressureGas[i].preNodeValue=m_controls.InitialP;
             }
             if(m_modelFlag.compareTo(ModelType.AddNoLocal)>=0)
             {
                  m_tGas[i].preNodeValue =m_initialTInner-(m_initialTInner-m_initialTOuter)*i/(m_nx-1);
                 //m_tGas[i].preNodeValue = m_temperatureInner-(m_temperatureInner-m_temperatureOuter)*i/m_nx;
             }
             if(m_modelFlag.compareTo(ModelType.AddVirus)>=0)
             {
                 m_cVirusInLiqWater[i].preNodeValue=m_controls.VirusInitial; //1.0e-10;
                 m_vVirus[i].preNodeValue=0.0;
                 m_vGas[i].preNodeValue -= m_vVirus[i].preNodeValue;
             }
         }// end for i

         //revise
         /* when adding the virus, the outer of mask will be spray virus, outer spot initial value will be changed*/
         if(m_modelFlag.compareTo(ModelType.AddVirus)>=0)
         {
              m_cVirusInLiqWater[m_nx-1].preNodeValue =(0.02*0.5
                                                       + m_cVirusInLiqWater[m_nx-1].preNodeValue
                                                       *m_vLiquidWater[m_nx-1].preNodeValue)/0.5;
              m_cVirusInLiqWater[m_nx-2].preNodeValue =(0.02*0.5
                                                       + m_cVirusInLiqWater[m_nx-2].preNodeValue
                                                       *m_vLiquidWater[m_nx-2].preNodeValue)/0.5;
              m_vLiquidWater[m_nx-1].preNodeValue = 0.2;
              m_vLiquidWater[m_nx-2].preNodeValue = 0.2;

              m_vGas[m_nx-1].preNodeValue =m_porosity- m_vLiquidWater[m_nx-1].preNodeValue;
              m_vGas[m_nx-2].preNodeValue =m_porosity- m_vLiquidWater[m_nx-2].preNodeValue;
         }

         for(int i=0;i<m_nx;i++)
         {
             /* Initilization for the fiber moisture concentration*/
             m_differentialWaterVapor[i] =0.0;
             m_surfaceVolumeRatio[i]=2.0*m_vFibers*m_vGas[i].preNodeValue/(m_porosity*m_meanFiberRadius);
             /* for sum*/
             m_meanCWaterVaporInFibers[i] = 0.0;
             m_meanCLiquidWaterInFibers[i] = 0.0;
             m_volumetricHeatCapacity[i]=0.0;
             m_specificHeatCapacity[i]=0.0;
             m_heatCoeSorptionWaterVapor[i]=0.0;
             m_heatCoeSorptionLiqWater[i]=0.0;
             m_meanFiberK=0;
             for (int j=0; j<m_includedFibers.size();j++)
             {

                  fiberItem=(TNumericalFiber)m_includedFibers.get(j);

                
                  fiberItem.m_wWaterVaporInFiber[i] = TMathPro.I_Interpolate(fiberItem.m_regainX,
                                                                          fiberItem.m_regainY,
                                                                          m_RH[i]);


                  fiberItem.m_wLiquidWaterInFiber[i] = All.ZERO;
                  fiberItem.m_specificHeatCapacity[i] = (0.32+fiberItem.m_wWaterVaporInFiber[i])
                                                         /(1+fiberItem.m_wWaterVaporInFiber[i]);

                  fiberItem.m_volumetricHeatCapacity[i]= fiberItem.m_density*fiberItem.m_specificHeatCapacity[i];

                  m_volumetricHeatCapacity[i] += fiberItem.m_volumetricHeatCapacity[i]*fiberItem.m_massRatio;
                  m_specificHeatCapacity[i] += fiberItem.m_specificHeatCapacity[i]*fiberItem.m_massRatio;

                  m_heatCoeSorptionWaterVapor[i] +=fiberItem.GetHeatCoeSorptionWaterVapor(i,fiberItem.m_wWaterVaporInFiber[i])*fiberItem.m_massRatio;


                  m_meanFiberK += fiberItem.GetThermolConductivity(fiberItem.m_wWaterVaporInFiber[i])*fiberItem.m_massRatio ;  //bug

                  for(int k=0;k<fiberItem.m_nr;k++)
                  {

                       fiberItem.m_cWaterVaporInFiber[i][k].preNodeValue=fiberItem.m_density*fiberItem.m_wWaterVaporInFiber[i];

                       if(m_modelFlag.compareTo(ModelType.AddLiquid)>=0)
                       {
                           fiberItem.m_cLiquidWaterInFiber[i][k].preNodeValue=fiberItem.m_density*fiberItem.m_wWaterVaporInFiber[i];

                       }
                  }

                  fiberItem.m_cMeanWaterVaporInFiber[i]=fiberItem.m_cWaterVaporInFiber[0][0].preNodeValue;
                  m_meanCWaterVaporInFibers[i]+= fiberItem.m_cMeanWaterVaporInFiber[i]
                                                               *fiberItem.m_massRatio;
                  if(m_modelFlag.compareTo(ModelType.AddLiquid)>=0)
                  {
                       fiberItem.m_cMeanLiquidWaterInFiber[i]= fiberItem.m_cLiquidWaterInFiber[0][0].preNodeValue;
                       m_meanCLiquidWaterInFibers[i] += fiberItem.m_cMeanLiquidWaterInFiber[i]*fiberItem.m_massRatio;
                       m_heatCoeSorptionLiqWater[i] +=fiberItem.GetHeatCoeSorptionLiquidWater(i,fiberItem.m_wWaterVaporInFiber[i])*fiberItem.m_massRatio;
                  }

             } //end for j fiber

             /* cv, kmix, cp*/

             m_volumetricHeatCapacity[i] = m_volumetricHeatCapacity[i]*m_vFibers
                                           + gas.m_density* gas.m_sHeatCapacity *m_vGas[i].preNodeValue;

             m_thermoConductivity[i] = m_meanFiberK//*m_vFibers
                                       +gas.m_thermoConductivityGas*m_vGas[i].preNodeValue;
             

             if(m_modelFlag.compareTo(ModelType.AddLiquid)>=0)
             {

                  m_volumetricHeatCapacity[i] +=liquidWater.m_density*liquidWater.m_sHeatCapacity*m_vLiquidWater[i].preNodeValue;

                  m_thermoConductivity[i]+=liquidWater.m_thermoConductivity*m_vLiquidWater[i].preNodeValue;
                   
                  /*DL*/  //revise
                  m_diffusionCoeLiqWater[i]=3.0*liquidWater.m_surfaceTension
                                            *Math.cos(m_meanFiberContactAngle*3.14159/180.0)
                                            *m_effectivePoreRadius
                                            *Math.pow(Math.sin(m_averageCapillaryAngle*3.14159/180.0),2)
                                            *m_vLiquidWater[i].preNodeValue
                                            *liquidWater.m_density
                                            /(40.0*m_porosity*liquidWater.m_dynamicViscosity);
             }

             if(m_modelFlag.compareTo(ModelType.AddNoLocal)>=0)
             {
                  m_thermoConductivityGas[i] = gas.m_thermoConductivityGas * m_vGas[i].preNodeValue ;
                  m_volumetricHeatCapacity[i] -= gas.m_density * gas.m_sHeatCapacity *m_vGas[i].preNodeValue;
                  m_thermoConductivity[i] -= gas.m_thermoConductivityGas*m_vGas[i].preNodeValue;

             }
         }// end for i

     };
     
     void EquationsCoefficient(TNumericalGas gas,TNumericalLiquidWater liquid,TNumericalVirus virus,int cycle, boolean IsHuman) {
    	 if(IsHuman)
         {
             if(m_layNumber!=0)  //question  when there is no body
             {
                 m_CVaporInner = TPhysicalPro.WaterVaporConcentration(m_temperatureInner)* m_RHInner;
             }
         }
         else
         {
             m_CVaporInner = TPhysicalPro.WaterVaporConcentration(m_temperatureInner)* m_RHInner;
         }
         m_CVaporOuter = TPhysicalPro.WaterVaporConcentration(m_temperatureOuter)*m_RHOuter;

         if(m_includedMembranes[0] != null)
         {
             if(m_tFabric[0].preNodeValue<273.16)
             {
                 m_includedMembranes[0].WVP = m_includedMembranes[0].WVPY.get(0);
                 m_includedMembranes[0].TR = m_includedMembranes[0].TRYY.get(0);
             }
             else
             {
                 m_includedMembranes[0].WVP = TMathPro.Interpolate(m_includedMembranes[0].WVPX,m_includedMembranes[0].WVPY,m_tFabric[0].preNodeValue);
                 m_includedMembranes[0].TR = TMathPro.Interpolate(m_includedMembranes[0].TRX,m_includedMembranes[0].TRYY,m_tFabric[0].preNodeValue);
             }
             m_includedMembranes[0].WR = 1/m_includedMembranes[0].WVP*8.64*1.0e+8*Math.abs(m_cWaterVapor[0].preNodeValue-m_CVaporInner);
             m_coeConvecMassIn=1.0/(m_includedMembranes[0].WR+(1.0/m_coeConvecMassIn));
             m_coeConvecHeatIn=1.0/(m_includedMembranes[0].TR+(1.0/m_coeConvecHeatIn));


         }
         
         if(m_includedMembranes[1] != null)
         {
             if(m_tFabric[m_nx-1].preNodeValue<273.16)
             {
                 m_includedMembranes[1].WVP = m_includedMembranes[1].WVPY.get(0);
                 m_includedMembranes[1].TR = m_includedMembranes[1].TRYY.get(0);
             }
             else
             {
                 m_includedMembranes[1].WVP = TMathPro.Interpolate(m_includedMembranes[1].WVPX,m_includedMembranes[1].WVPY,m_tFabric[m_nx-1].preNodeValue);
                 m_includedMembranes[1].TR = TMathPro.Interpolate(m_includedMembranes[1].TRX,m_includedMembranes[1].TRYY,m_tFabric[m_nx-1].preNodeValue);
             }
             m_includedMembranes[1].WR = 8.64*1.0e+8*Math.abs(m_cWaterVapor[m_nx-1].preNodeValue-m_CVaporOuter)/m_includedMembranes[1].WVP;
             m_coeConvecMassOut = 1.0/(m_includedMembranes[1].WR+(1.0/m_coeConvecMassOut));
             m_coeConvecHeatOut = 1.0/(m_includedMembranes[1].TR+(1.0/m_coeConvecHeatOut));
         }

         for(int i=0;i<m_nx;i++)
         {

              /* caculate the main items in the equations*/
              /* sss1*/
              m_absorbedWaterVapor[i] = 1*m_vFibers*m_differentialWaterVapor[i];
              /* ttt1*/
              m_heatLossByFibers[i] = m_heatCoeSorptionWaterVapor[i]
                                    *m_vFibers* m_differentialWaterVapor[i];

              /* D*/
              m_coeWaterVapor[i] = gas.m_diffusionCoeWaterVapor*m_vGas[i].preNodeValue;

              m_waterVaporItem[i] = m_absorbedWaterVapor[i];
              m_temperatureItem[i] = m_heatLossByFibers[i];
              

              if(m_modelFlag.compareTo(ModelType.AddLiquid)>=0)
              {

                  m_vSorbtionProportion = (1.0-Math.pow((m_vLiquidWater[i].preNodeValue/m_porosity),0.3))
                                             *m_vGas[i].preNodeValue/m_porosity;
                  m_lSorbtionProportion = 1-m_vSorbtionProportion;
                  if(m_vLiquidWater[i].preNodeValue >= 1.0e-2)
                  {
                       m_absorbedWaterVapor[i] = m_vFibers*m_differentialWaterVapor[i]*m_vSorbtionProportion;
                       m_heatLossByFibers[i] = m_heatCoeSorptionWaterVapor[i]
                                             *m_vFibers* m_differentialWaterVapor[i]
                                             *m_vSorbtionProportion + m_heatCoeSorptionLiqWater[i]
                                             *m_vFibers*m_differentialLiquidWater[i]
                                             *m_lSorbtionProportion;
                       m_cSaturatedWaterVapor[i] = TPhysicalPro.WaterVaporConcentration(m_tFabric[i].preNodeValue);
                       m_volumeEvapCondensation[i] = m_coeMassTransfer *(m_cSaturatedWaterVapor[i]-m_cWaterVapor[i].preNodeValue)
                                                * m_surfaceVolumeRatio[i];
                       if(m_volumeEvapCondensation[i]>1e+10 || m_volumeEvapCondensation[i]>1e-10)
                       {
                            m_volumeEvapCondensation[i] = 0;
                       }
                       m_heatEvapCondensation[i] = -liquid.GetLatentHeatEvaporation(m_tFabric[i].preNodeValue) * m_volumeEvapCondensation[i];
                       /* ssss*/
                       m_liquidWaterItem[i] = m_volumeEvapCondensation[i]+m_vFibers*m_differentialLiquidWater[i]
                                             *m_lSorbtionProportion;
                  }
                  else
                  {
                       m_absorbedWaterVapor[i] = m_vFibers*m_differentialWaterVapor[i];
                       m_heatLossByFibers[i] = m_heatCoeSorptionWaterVapor[i]
                                             *m_vFibers* m_differentialWaterVapor[i];
                       m_volumeEvapCondensation[i] = 0;
                       m_heatEvapCondensation[i] = 0;
                       m_liquidWaterItem[i] = 0;

                  }

                  m_waterVaporItem[i] = m_absorbedWaterVapor[i]- m_volumeEvapCondensation[i];

                  m_temperatureItem[i] = m_heatLossByFibers[i] + m_heatEvapCondensation[i];

              } // liquid water

              if(m_modelFlag.compareTo(ModelType.AddPressure)>=0)
              {
                  /*G*/
                  m_coePressureWaterVapor[i] = m_cWaterVapor[i].preNodeValue *3.0*Math.pow(m_effectivePoreRadius,2)
                                             *Math.pow(Math.sin(m_averageCapillaryAngle*3.14159/180.0),2)*m_porosity
                                             *(1.0-Math.pow((m_vLiquidWater[i].preNodeValue/m_porosity),3))
                                             /(80.0* gas.m_dynamicViscosity);

                  /*GL*/
                  m_coePressureLiquidWater[i] = liquid.m_density* 3.0*Math.pow(m_effectivePoreRadius,2)
                                             *Math.pow(Math.sin(m_averageCapillaryAngle*3.14159/180.0),2)*m_porosity
                                             *Math.pow((m_vLiquidWater[i].preNodeValue/m_porosity),3)
                                             /(80.0* liquid.m_dynamicViscosity);

                  /* GS*/
                  m_coePressureGas[i] = gas.m_moleMassGas*m_pressureGas[i].preNodeValue *3.0*Math.pow(m_effectivePoreRadius,2)
                                      *Math.pow(Math.sin(m_averageCapillaryAngle*3.14159/180.0),2)*m_porosity
                                      *(1.0-Math.pow((m_vLiquidWater[i].preNodeValue/m_porosity),3))
                                      /(80.0*gas.m_dynamicViscosity*gas.m_gasConstant * m_tFabric[i].preNodeValue);

                   /*A3*/
                   
                   m_coePressureTime[i] = m_vGas[i].preNodeValue * gas.m_moleMassGas
                                        /(gas.m_gasConstant* m_tFabric[i].preNodeValue);

                   /*A4*/
                   m_coePressureTempetratureToTime[i] = -m_pressureGas[i].preNodeValue * m_vGas[i].preNodeValue
                                                      * gas.m_moleMassGas
                                                      /(gas.m_gasConstant*Math.pow(m_tFabric[i].preNodeValue,2));

                   /* A5 */
                   m_coePressureLiquidWaterTotime[i] = -m_pressureGas[i].preNodeValue*gas.m_moleMassGas
                                                     /(gas.m_gasConstant*m_tFabric[i].preNodeValue);

              }  //pressure

              if(m_modelFlag.compareTo(ModelType.AddVirus)>=0)
              {
                   virus.GetVelocityBreathOut(cycle);
                   /* DV */
                   m_coeDiffusionVirus[i] = virus.m_BrownCoe * m_vLiquidWater[i].preNodeValue;
                   /* GV */
                   m_coeVirusToPressure[i] = m_cVirusInLiqWater[i].preNodeValue * 3.0
                                          * Math.pow(Math.sin(m_averageCapillaryAngle*3.14159/180.0),2)
                                          * Math.pow(m_effectivePoreRadius,2)*Math.pow(m_vLiquidWater[i].preNodeValue/m_porosity,3)
                                          * m_porosity/(80 * liquid.m_dynamicViscosity);

                  /* DLV*/
                  m_coeVirusToLiqWater[i] = m_cVirusInLiqWater[i].preNodeValue * m_diffusionCoeLiqWater[i]/liquid.m_density;

                  /* Blp*/ 
                  m_coePVelocityLiq[i] = -m_coePressureLiquidWater[i]/m_vLiquidWater[i].preNodeValue;

                  /* Bll*/
                  m_coeVVelocityLiq[i] = -m_diffusionCoeLiqWater[i]/m_vLiquidWater[i].preNodeValue;

                  /* Vl*/
                  if(i==0)
                  {
                      liquid.m_velocity= Math.abs(2*(m_coePVelocityLiq[1]*(m_pressureGas[2].preNodeValue - m_pressureGas[0].preNodeValue)
                                          + m_coeVVelocityLiq[1]*(m_vLiquidWater[2].preNodeValue -m_vLiquidWater[0].preNodeValue))/(2.0*m_deltaX)
                                          -(m_coePVelocityLiq[2]*(m_pressureGas[3].preNodeValue - m_pressureGas[1].preNodeValue)
                                          + m_coeVVelocityLiq[2]*(m_vLiquidWater[3].preNodeValue -m_vLiquidWater[1].preNodeValue))/(2.0*m_deltaX));
                  }
                  else if(i==m_nx-1)
                  {
                      liquid.m_velocity= Math.abs(2*(m_coePVelocityLiq[m_nx-2]*(m_pressureGas[m_nx-1].preNodeValue - m_pressureGas[m_nx-3].preNodeValue)
                                          + m_coeVVelocityLiq[m_nx-2]*(m_vLiquidWater[m_nx-1].preNodeValue -m_vLiquidWater[m_nx-3].preNodeValue))/(2.0*m_deltaX)
                                          -(m_coePVelocityLiq[m_nx-3]*(m_pressureGas[m_nx-2].preNodeValue - m_pressureGas[m_nx-4].preNodeValue)
                                          + m_coeVVelocityLiq[m_nx-3]*(m_vLiquidWater[m_nx-2].preNodeValue -m_vLiquidWater[m_nx-4].preNodeValue))/(2.0*m_deltaX));
                  }
                  else
                  {
                      liquid.m_velocity= Math.abs((m_coePVelocityLiq[i]*(m_pressureGas[i+1].preNodeValue - m_pressureGas[i-1].preNodeValue)
                                          + m_coeVVelocityLiq[i]*(m_vLiquidWater[i+1].preNodeValue -m_vLiquidWater[i-1].preNodeValue))/(2.0*m_deltaX));
                  }

                  /* K1, K2 */
                  if(m_effectivePoreRadius > virus.m_radiusVirusParticle)
                  {
                      virus.m_coeDepositionRate=(virus.m_effectiveCoeCapture * virus.m_retentionEfficiency * m_surfaceVolumeRatio[i]
                                                 + virus.m_adjustableParameter * m_vVirus[i].preNodeValue)* liquid.m_velocity;
                      virus.m_coeRelease = virus.releaseCoeVirus * (liquid.m_velocity - virus.m_criticalVelocity ) * m_vLiquidWater[i].preNodeValue;
                  }
                  else
                  {
                      virus.m_coeDepositionRate=liquid.m_velocity;
                      virus.m_coeRelease =1.0e-6;
                  }

                  m_differentialVirus[i]= virus.m_coeDepositionRate*m_vLiquidWater[i].preNodeValue * m_cVirusInLiqWater[i].preNodeValue *m_porosity
                                         - virus.m_coeRelease *m_vVirus[i].preNodeValue * virus.m_density;

                  m_vVirus[i].nextNodeValue =  m_differentialVirus[i]* m_deltaT / virus.m_density + m_vVirus[i].preNodeValue ;

                  if(m_vVirus[i].nextNodeValue <0)
                  {
                      m_vVirus[i].nextNodeValue =0.0;
                  }

              } //end virus

              if(m_modelFlag.compareTo(ModelType.AddNoLocal)>=0)
              {
                  /* Bsp*/
                  m_coePVelocityGas[i]= -3.0*Math.pow(m_effectivePoreRadius,2)*Math.pow(Math.sin(m_averageCapillaryAngle*3.14159/180.0),2)*m_porosity
                                        *(1-Math.pow(m_vLiquidWater[i].preNodeValue /m_porosity,3))/(80.0* gas.m_dynamicViscosity * m_vGas[i].preNodeValue);
                  /* a(i)*/

                  m_coeLocalEquiGas[i]= m_vGas[i].preNodeValue * (m_cWaterVapor[i].preNodeValue*gas.m_pHeatCapacityVapor
                                        + (m_pressureGas[i].preNodeValue*gas.m_moleMassGas /(m_tGas[i].preNodeValue*gas.m_gasConstant)
                                        -gas.m_moleMassGas *m_cWaterVapor[i].preNodeValue /liquid.m_moleMassWater)*gas.m_pHeatCapacityDryAir);
                  /* s(i)*/
                  m_valueLocalEquiGas[i]=  -m_coeHeatGasFiber * m_vGas[i].preNodeValue * m_surfaceVolumeRatio[i]*(m_tGas[i].preNodeValue - m_tFabric[i].preNodeValue); //revise

                  /* vs */
                  if(i==0)
                  {
                      gas.m_velocity.set(i, m_coeLocalEquiGas[i]*(2*m_coePVelocityGas[1]*(m_pressureGas[2].preNodeValue -m_pressureGas[0].preNodeValue)/(2*m_deltaX)
                                         -m_coePVelocityGas[2]*(m_pressureGas[3].preNodeValue -m_pressureGas[1].preNodeValue)/(2*m_deltaX)));    //revise
                  }
                  else if(i==m_nx-1)
                  {
                      gas.m_velocity.set(i, m_coeLocalEquiGas[i]*(2*m_coePVelocityGas[m_nx-2]*(m_pressureGas[m_nx-1].preNodeValue -m_pressureGas[m_nx-3].preNodeValue)/(2*m_deltaX)
                                         -m_coePVelocityGas[m_nx-3]*(m_pressureGas[m_nx-2].preNodeValue -m_pressureGas[m_nx-4].preNodeValue )/(2*m_deltaX)));

                  }
                  else
                  {
                      gas.m_velocity.set(i, m_coeLocalEquiGas[i]*(m_coePVelocityGas[i]*(m_pressureGas[i+1].preNodeValue -m_pressureGas[i-1].preNodeValue)/(2*m_deltaX)));

                  }

                  /* Fe, Fw,   */
                  if(i==0)
                  {
                       //m_coeFeTg[i]= TMathPro::Max(-(gas.m_velocity[0]+gas.m_velocity[1])/2.0,0);
                       m_coeFeTg[i]=(gas.m_velocity.get(0)+gas.m_velocity.get(1))/2.0; //revise
                       if(-m_coeFeTg[i]<0)
                       {
                            m_coeFeTg[i]=0;
                       }
                       m_coeDDeTg[i]= (m_thermoConductivityGas[0] + m_thermoConductivityGas[1])/(2* m_deltaX);
                       m_coePeTg= m_coeFeTg[i]/ m_coeDDeTg[i];
                       //m_coeAfeTg[i]=TMathPro::Max(pow(1-0.1*fabs(m_coePeTg),5),0); //revise
                       m_coeAfeTg[i]=Math.pow(1-0.1*Math.abs(m_coePeTg),5);
                       if(m_coeAfeTg[i]<0)
                       {
                            m_coeAfeTg[i]=0;
                       }
                  }
                  else if(i==m_nx-1)
                  {
                       //m_coeFwTg[i]=TMathPro::Max((gas.m_velocity[m_nx-1]+gas.m_velocity[m_nx-2])/2,0); revise
                       m_coeFwTg[i]=(gas.m_velocity.get(m_nx-1)+gas.m_velocity.get(m_nx-2))/2.0;
                       if(m_coeFwTg[i]<0)
                       {
                            m_coeFwTg[i]=0;
                       }
                       m_coeDDwTg[i]=(m_thermoConductivityGas[m_nx-1] + m_thermoConductivityGas[m_nx-2])/(2* m_deltaX);
                       m_coePwTg=m_coeFwTg[i]/m_coeDDwTg[i];
                       //m_coeAfwTg[i]=TMathPro::Max(pow(1-0.1*fabs(m_coePwTg),5),0); revise
                       m_coeAfwTg[i]=Math.pow(1-0.1*Math.abs(m_coePwTg),5);
                       if(m_coeAfwTg[i]<0)
                       {
                            m_coeAfwTg[i]=0;
                       }

                  }
                  else
                  {
                       //m_coeFeTg[i]= TMathPro::Max(-(gas.m_velocity[i]+gas.m_velocity[i+1])/2.0,0);
                       m_coeFeTg[i]=(gas.m_velocity.get(i)+gas.m_velocity.get(i+1))/2.0; //revise
                       if(-m_coeFeTg[i]<0)
                       {
                            m_coeFeTg[i]=0;
                       }
                       //m_coeFwTg[i]= TMathPro::Max((gas.m_velocity[i]+gas.m_velocity[i-1])/2.0,0);
                       m_coeFwTg[i]=(gas.m_velocity.get(i)+gas.m_velocity.get(i-1))/2.0;
                       if(m_coeFwTg[i]<0)
                       {
                            m_coeFwTg[i]=0;
                       }
                       m_coeDDeTg[i]= (m_thermoConductivityGas[i]+ m_thermoConductivityGas[i+1])/(2* m_deltaX);
                       m_coePeTg= m_coeFeTg[i]/ m_coeDDeTg[i];
                       //m_coeAfeTg[i]=TMathPro::Max(pow(1-0.1*fabs(m_coePeTg),5),0);
                       m_coeAfeTg[i]=Math.pow(1-0.1*Math.abs(m_coePeTg),5);
                       if(m_coeAfeTg[i]<0)
                       {
                            m_coeAfeTg[i]=0;
                       }
                       m_coeDDwTg[i]=(m_thermoConductivityGas[i]+ m_thermoConductivityGas[i-1])/(2* m_deltaX);
                       m_coePwTg=m_coeFwTg[i]/m_coeDDwTg[i];
                       //m_coeAfwTg[i]=TMathPro::Max(pow(1-0.1*fabs(m_coePwTg),5),0);
                       m_coeAfwTg[i]=Math.pow(1-0.1*Math.abs(m_coePwTg),5);
                       if(m_coeAfwTg[i]<0)
                       {
                            m_coeAfwTg[i]=0;
                       }
                       m_coeFweTg[i]=(gas.m_velocity.get(i)+gas.m_velocity.get(i+1))/2.0-(gas.m_velocity.get(i)+gas.m_velocity.get(i-1))/2.0;
                  }
              }
         } // end for i

         /* boundary condition */
         if(m_modelFlag.compareTo(ModelType.AddLiquid)>=0)
         {

             m_CLiquidInner=(TPhysicalPro.WaterVaporConcentration(m_tFabric[0].preNodeValue)-m_CVaporInner)
                            *m_vLiquidWater[0].preNodeValue *m_coeMassTransfer*m_deltaX/m_porosity;

             m_CLiquidOuter=(TPhysicalPro.WaterVaporConcentration(m_tFabric[m_nx-1].preNodeValue)-m_CVaporOuter)
                            *m_vLiquidWater[m_nx-1].preNodeValue *m_coeMassTransfer*m_deltaX/m_porosity;

             m_heatInner=liquid.GetLatentHeatEvaporation(m_tFabric[0].preNodeValue)*m_CLiquidInner;

             m_heatOuter=liquid.GetLatentHeatEvaporation(m_tFabric[m_nx-1].preNodeValue)*m_CLiquidOuter;

         }
     };
     
     void BoundaryStatus(TNumericalGas gas,int layerCount,StageType stage,int i,int j,BoundaryFabric fabricSeg) {
    	 if(stage.SpaceLength[i].get(j) != null) //question
         {
             m_inContact=false;
         }
         else
         {
             m_inContact=true;
         }

         if(stage.SpaceLength[i].get(j+1) != null)
         {
             m_outContact=false;
         }
         else
         {
             m_outContact=true;
         }

    	 m_coeConvecMassIn = fabricSeg.m_massCoeInner;
    	 m_coeConvecHeatIn = fabricSeg.m_heatCoeInner;
    	 m_coeConvecMassOut = fabricSeg.m_massCoeOuter;
    	 m_coeConvecHeatOut = fabricSeg.m_heatCoeOuter; 
     };
	// void SetTransferCoe(int, int, int, int);

};
